package app.ui.panels {
	import app.Main;
	import app.model.Util;
	import app.ui.panels.event.EventUI;

	import bme.data.datatypes.BRC_Event;
	import bme.data.datatypes.BRC_Occurrence;

	import com.adobe.serialization.json.JSON;
	import com.bit101.components.PushButton;

	import flash.display.DisplayObjectContainer;

	/**
	 * @author Rendall
	 */
	public class EventBrowserPanel extends AbstractBrowserPanel {
		public static const STATE_OPTION_PUSH : String = "STATE_OPTION_PUSH";

		private var events : Array;
		private var eventsTime : Array;

		private var eventIndex : uint;
		private var eventui : EventUI;
		private var currEventui : EventUI;
		private var nextButton : PushButton;
		private var prevButton : PushButton;
		private var optionButton : PushButton;
		private var sortOption : String;
		private var lastIndex : uint;

		public function EventBrowserPanel(parent : DisplayObjectContainer = null, xpos : Number = 0, ypos : Number = 0) {
			super(parent, xpos, ypos);
			setUI();
			eventIndex = 0;
			EventUI.target = this;
		}

		private function setUI() : void {
			this.setSize(550, 400);
			
			setTitle("events");
			
			prevButton = new PushButton(this, 5, 400 - 30, "PREV", onPrevPush);
			nextButton = new PushButton(this, 0, prevButton.y, "NEXT", onNextPush);
			nextButton.x = 550 - nextButton.width - 5;
			
			optionButton = new PushButton(this, 275, 5, "OPTIONS", onOptionPush);
			optionButton.x = 550 - optionButton.width - 5;
		}

		private function onOptionPush(evt : * = null) : void {
			stateChangeSignal.dispatch(STATE_OPTION_PUSH);
		}

		private function onNextPush(evt : * = null) : void {
			eventIndex = Math.min(eventIndex + 1, lastIndex);
			trace("\n EventBrowserPanel.onNextPush(" + eventIndex + ")");
			showCurrentEvent();
		}

		private function onPrevPush(evt : * = null) : void {
			eventIndex = Math.max(eventIndex - 1, 0);
			trace("\n EventBrowserPanel.onPrevPush(" + eventIndex + ")");

			showCurrentEvent("prev");
		}

		public function getEventUI(index : uint) : EventUI {
			var eventui : EventUI;
			
			trace("\n EventBrowserPanel.getEventUI( index:" + index + ")");
			trace ("\tsortOption : " + sortOption);
			
			
			if (sortOption == "time") {
				var eventID : uint = eventsTime[index].id;
				trace ("\teventsTime[index].time : " + eventsTime[index].time);
				
				var event : BRC_Event = BRC_Event.getEventByID(eventID);
				eventui = EventUI.getPanel(event);
			}			
			else eventui = EventUI.getPanel(events[index]);
			
			return eventui;
		}

		public function sortBy(index : uint) : void {
			sortOption = Main.SORT_OPTIONS[index].option;
			trace("\n EventBrowserPanel.sortBy( option:" + sortOption + ")");
			trace("\tevents : " + events);
			
			lastIndex = events.length - 1;
			switch (sortOption) {
				case "shuffle":
					events = Util.shuffleArray(events);
					trace("shuffle was selected");
					break;                    
				case "camp":
					trace("camp was selected");
					events = events.sortOn("hosted_by_camp");
					break;
				case "id":
					events = events.sortOn("id", Array.NUMERIC);
					trace("id was selected");
					break;                    
				case "time":
					if (!eventsTime) {
						eventsTime = new Array();
						for each (var event : BRC_Event in events) {
							for each (var time : BRC_Occurrence in event.occurrence_set) {
								eventsTime.push({id:event.id, time:time.toString()});
							}
						}
						eventsTime = eventsTime.sortOn("time");
					}
					lastIndex = eventsTime.length - 1;
					
					if (!currEventui) eventIndex = 0;
					else for (eventIndex = 0;eventIndex <= lastIndex;eventIndex++) {
						//trace ("\teventIndex : " + eventIndex);
						//trace ("\t" + eventsTime[eventIndex].id + " == " + currEventui.event.id + " : " + Boolean(eventsTime[eventIndex].id == currEventui.event.id));
						
						
						if (eventsTime[eventIndex].id == currEventui.event.id) break;
					}
					trace("\teventIndex : " + eventIndex);
					
					trace("\teventsTime : " + JSON.encode(eventsTime));
					
					trace("time was selected");
					break;
				default:
					throw new Error("unknown sort item " + sortOption);
			}
			
			trace("\tevents : " + events);
			
			if (sortOption != "time") eventIndex = events.indexOf(currEventui.event);
			trace("\teventIndex : " + eventIndex, lastIndex);
			
			updateNavButtons();
		}

		
		
		public function setEventsArray(events : Array) : void {
			this.events = events;
		}

		public function showCurrentEvent(option : String = null) : void {
			if (currEventui) currEventui.hide(option);
			eventui = getEventUI(eventIndex);
			eventui.y = 60;
			eventui.x = 5;
			eventui.show(option);	
			currEventui = eventui;
			
			updateNavButtons();
			
			trace("\tevents.length : " + events.length);
			trace("\teventIndex : " + eventIndex);
		}

		private function updateNavButtons() : void {
			prevButton.enabled = (eventIndex != 0);
			nextButton.enabled = (eventIndex != lastIndex);
		}

		override public function show(option : *= null) : void {
			if (sortOption == null) sortBy(0);
			showCurrentEvent();
			super.show();
		}
	}
}
